from flask import render_template,jsonify,request,current_app, url_for,g
from models import Articleq
from . import root_bp

# 定义部分视图函数
@root_bp.route('/')
def index():
    artis=Articleq.query.order_by(Articleq.a_id.desc()).limit(5)
    if g.username==None:
        inxexd={'link1':url_for('user.loginin'),'link2':url_for('user.signup'),'arg1':'登录','arg2':'注册',"artis":artis}
    else:
        inxexd={'link2':url_for('user.profile',uid=g.uid),'link1':url_for('user.logout'),'arg2':g.username,'arg1':'注销',"artis":artis}
    return render_template("index.html",**inxexd)


@root_bp.route('/hello')
def hello():
    name = request.args.get('name', '')
    return 'Hello ' + name + '!'


# <id>可以改成<int:id>,声明id的数据类型为正整数（float为正浮点数，string不包含/的文本，path包含/的文本）
@root_bp.route('/numtst/<id>')
def numtst(id):
    return '<h1>'+str(id)+'</h1>'

    #打印config信息 app.config.items()返回一个由二元元组构成的列表
@root_bp.route('/cenv')
def cenv():
    tmp={(item[0]):item[1] for item in current_app.config.items()}
        
    return str(tmp)
    


    # 传输包含网站所有路由的json{视图函数:路径}
@root_bp.route('/umjson')
def umjson():
    umruleiter = current_app.url_map.iter_rules()
    return jsonify({umrule.endpoint: umrule.rule for umrule in umruleiter})


@root_bp.route('/test500')
def test():
    return render_template('testerror.html',errortext="The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.")